package org.wsiz.vro.web.domain;

import java.io.Serializable;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;
import org.wsiz.vro.domain.Role;
import org.wsiz.vro.repository.RoleRepository;
import org.wsiz.vro.web.domain.support.GenericSearchForm;

/**
 * SearchForm for {@link Role}
 */
@Component
@Scope("session")
public class RoleSearchForm extends GenericSearchForm<Role> implements Serializable {
    private static final long serialVersionUID = 1L;
    // make it static to avoid http://jira.springframework.org/browse/SWF-1224
    private static RoleRepository roleRepository;

    private Role role = new Role();

    @Autowired
    public RoleSearchForm(RoleRepository instance) {
        if (roleRepository == null) {
            roleRepository = instance;
        }
    }

    /**
     * Server side pagination with lazy model.
     * Automatically called by PrimeFaces component (via the GenericSearchForm).
     */
    @Override
    protected List<Role> load(PageRequest pageRequest) {
        Page<Role> page = roleRepository.findWithExample(role, pageRequest);
        setRowCount((int) page.getTotalElements());
        return page.getContent();
    }

    /**
     * The root role for search by example.
     * Used from the view.
     */
    public Role getRole() {
        return role;
    }
}